import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        users: [],
        dangerousBehaviors: [],
        dangerousPersons: [],
        strangers: []
    },
    mutations: {
        setDangerousBehaviors(state, behaviors) {
            state.dangerousBehaviors = behaviors;
        },
        setDangerousPersons(state, persons) {
            state.dangerousPersons = persons;
        },
        setStrangers(state, strangers) {
            state.strangers = strangers;
        },
        setUsers(state, users) {
            state.users = users;
        },
        addUser(state, user) {
            state.users.push(user);
        },
        updateUser(state, updatedUser) {
            const index = state.users.findIndex(user => user.id === updatedUser.id);
            if (index !== -1) {
                Vue.set(state.users, index, updatedUser);
            }
        },
        deleteUser(state, userId) {
            state.users = state.users.filter(user => user.id !== userId);
        },
        deleteDangerousBehavior(state, behaviorId) {
            state.dangerousBehaviors = state.dangerousBehaviors.filter(behavior => behavior.id !== behaviorId);
        }
    },
    actions: {
        fetchUsers({ commit }) {
            return axios.get('/api/users')
                .then(response => {
                    commit('setUsers', response.data);
                })
                .catch(error => {
                    console.error('Error fetching users:', error);
                });
        },
        createUser({ commit }, user) {
            return axios.post('/api/users', user)
                .then(response => {
                    commit('addUser', response.data);
                })
                .catch(error => {
                    console.error('Error creating user:', error);
                });
        },
        updateUser({ commit }, user) {
            return axios.put(`/api/users/${user.id}`, user)
                .then(response => {
                    commit('updateUser', response.data);
                })
                .catch(error => {
                    console.error('Error updating user:', error);
                });
        },
        deleteUser({ commit }, userId) {
            return axios.delete(`/api/users/${userId}`)
                .then(() => {
                    commit('deleteUser', userId);
                })
                .catch(error => {
                    console.error('Error deleting user:', error);
                });
        },
        fetchDangerousBehaviors({ commit }) {
            return axios.get('/api/dangerous-behaviors')
                .then(response => {
                    commit('setDangerousBehaviors', response.data);
                })
                .catch(error => {
                    console.error('Error fetching dangerous behaviors:', error);
                });
        },
        fetchDangerousPersons({ commit }) {
            return axios.get('/api/dangerous-persons')
                .then(response => {
                    commit('setDangerousPersons', response.data);
                })
                .catch(error => {
                    console.error('Error fetching dangerous persons:', error);
                });
        },
        deleteDangerousBehavior({ commit }, behaviorId) {
            return axios.delete(`/api/dangerous-behaviors/${behaviorId}`)
                .then(() => {
                    commit('deleteDangerousBehavior', behaviorId);
                })
                .catch(error => {
                    console.error('Error deleting dangerous behavior:', error);
                });
        },
        deleteDangerousPerson(_, personId) {
            return axios.delete(`/api/dangerous-persons/${personId}`)
                .then(() => {
                    // Optionally commit a mutation or handle response as needed
                })
                .catch(error => {
                    console.error('Error deleting dangerous person:', error);
                    throw error; // Ensure error is propagated for handling in component
                });
        }
    }
});
